package com.example.user.service.domain;

import com.example.user.service.common.IdWorker;
import com.example.user.service.domain.port.GetUserByNamePort;
import com.example.user.service.domain.port.GetUserListPort;
import com.example.user.service.domain.valueobject.*;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
@Setter
@Getter
@ToString
public class User {
    private UserId id;
    private UserName name;
    private UserAge age;
    private Email email;
    private Password password;
    private Phone phone;
    private RealName realName;
    private Status status;
    private Role role;
    private CreateTime createTime;
    private UpdateTime updateTime;
    private LastLoginTime lastLoginTime;
    public User() {
    }

    public User(UserId id, UserName name, UserAge age, Email email, Password password) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.email = email;
        this.password = password;
    }

    public User( UserName name, UserAge age, Email email, Password password) {
        this.id= genId() ;
        this.name = name;
        this.age = age;
        this.email = email;
        this.password = password;

    }

    public User(UserId userId, UserName userName, UserAge userAge, Email email) {
        this.id = userId;
        this.name = userName;
        this.age = userAge;
        this.email = email;

    }

    public User(UserId userId, UserName userName, Email email, Password password) {
        this.id = userId;
        this.name = userName;
        this.email = email;
        this.password = password;
    }


    public static List<User> getUsers(GetUserListPort getUserListPort){
        return getUserListPort.getUsers();
    }

    /**
     * 根据用户名查询用户
     * 当需要使用类似GetUserByNamePort这种对象的时候，需要在方法参数注入该对象
     * 因为通过构造方法或者字段注入都会失败，因为方法是静态方法，会早于对象创建，导致对象无法注入
     * @param name 用户名
     * @param getUserByNamePort 查询用户的端口
     * @return 用户模型
     */
    public static User getUserByName(String name, GetUserByNamePort getUserByNamePort){
        User user = getUserByNamePort.getUserByName(name);
        log.info("user:{}", user);
        return user;
    }

    public UserId genId(){
        return new UserId(new IdWorker().nextId());
    }

    /**
     * 验证密码
     * @param password 密码 明文还是密文？
     * @return 验证结果
     */
    public boolean validatePassword(String password){
        return this.password.verify( password);
    }
}
